﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Home.Master" AutoEventWireup="true" CodeBehind="Bid.aspx.cs" Inherits="OnlineAuction.Bid" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <%--<script src="Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>--%>
    <script>
        var $jq = jQuery.noConflict();
        var second = '<%=auction.LeftSeconds%>';
        var minute = '<%=auction.LeftMinutes%>';
        var hour = '<%=auction.LeftHours%>';
        var day = '<%=auction.LeftDays%>';
        var lastPrice;
        var buyer;
        var myVar = setInterval(function () { myTimer() }, 1000);
        function myTimer() {
            var d = new Date();
            var t = d.toLocaleTimeString();
            document.getElementById('<%=lbLeftSeconds.ClientID%>').innerHTML = second;
            document.getElementById('<%=lbLeftMinutes.ClientID%>').innerHTML = minute;
            document.getElementById('<%=lbLeftHours.ClientID%>').innerHTML = hour;
            document.getElementById('<%=lbLeftDay.ClientID%>').innerHTML = day;
            // $j('#lbLeftSeconds').html(serverTime);
            //count down 
            second--;
            if (second <= 0) {
                if (minute > 0) {
                    minute--;
                    second = 59;
                } else if (minute <= 0) {
                    if (hour > 0) {
                        hour--;
                        minute = 59;
                        second = 59;
                    } else if (hour <= 0) {
                        if (day > 0) {
                            day--;
                            hour = 23;
                            minute = 59;
                            second = 59
                        }
                        else if (day <= 0) {
                            document.getElementById('<%=lbLeftSeconds.ClientID%>').innerHTML = 0;
                            document.getElementById('<%=lbLeftMinutes.ClientID%>').innerHTML = 0;
                            document.getElementById('<%=lbLeftHours.ClientID%>').innerHTML = 0;
                            document.getElementById('<%=lbLeftDay.ClientID%>').innerHTML = 0;
                            alert("END");
                            myStopFunction();
                        }
                }
        }
}
            //check left 10 seconds
    if (day == 0 && hour == 0 && minute == 0) {
        if (second <= 10) {
            $jq.get("/Ajax/GetTime.aspx?AuctionID=" + "<%=aid%>", function (response) {
                $jq("#<%=lbLeftSeconds.ClientID%>").html(response);
            });
        }
    }
            //update price per second
    $jq.get("/Ajax/GetPrice.aspx?AuctionID=" + "<%=aid%>", function (response) {
                var res = response.toString();
                buyer = res.split("|")[0];
                lastPrice = Number(res.split("|")[1]);
                // alert (lastPrice);
                $jq("#<%=lbLastBidPerson.ClientID%>").html("Top bidder: " + buyer);
                $jq("#<%=lbBidPrice.ClientID%>").html("Current price: " + lastPrice + "$");
        // check coookie with current top
        if ('<%=ck%>' == buyer) {
            //   $jq("#ibtnBid").css("display", "none");
        }
    });
}
function myStopFunction() {
    clearInterval(myVar);
}
//bid click
$jq(document).ready(function () {
    $jq("#ibtnBid").click(function () {
        //check ban nick
        //   if ('< %=auction.%>' == '0') {
        //     alert("Your account is blocked!");
        //}
        if (day == 0 && hour == 0 && minute == 0) {
            if (second <= 10) {
                //alert("<10s");
                updateAuctionHistory();
                updateFinishTime();
            }
        } else {
            //alert(">10sSS");
            updateAuctionHistory();
        }
    });
});

function updateAuctionHistory() {
    var price = 0;
    if ('<%=isNormal%>' == 'true')
        price = $jq("#<%=txtBidPrice.ClientID%>").val();
    else price = lastPrice + Number('<%=auction.Increment.ToString()%>');
    //when click bid
    if (price <= lastPrice) {
        alert("Your price are so low!");        
        return;
    }
    else {
        //check money on server
        $jq.get("/Ajax/UpdateAuctionHistory.aspx?AuctionID=" + "<%=aid%>" + "&price=" + price
            + "&lBuyer=" + buyer + "&lPrice=" + lastPrice, function (response) {
                if (response == "nck") {
                    location.replace("Home.aspx");
                }
                if (response != 'ok') {
                    alert(response);
                    
                    return;
                }
            });
    }
}
//when bid in less than 10 seconds
function updateFinishTime() {
    $jq.get("/Ajax/UpdateFinishTime.aspx?AuctionID=" + "<%=aid%>", function (response) { });
}
    </script>

</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
    <div style="width: 100%; margin: 10px; height: auto;">
        <div style="float: left; display: inline; width: 50%; height: auto">
            <asp:Image ID="imgItemImage1" runat="server" Width="100%" Height="250px" />
        </div>
        <div style="float: left; display: inline; width: 50%; height: auto">
            <asp:Label ID="lbItemName" runat="server" Text="Label" />
            <br />
            <br />
            <asp:Label ID="lbBidPrice" runat="server" Text="Label" />
            <br />
            <br />
            <asp:Label ID="lbLastBidPerson" runat="server" Text="Label" />
            <br />
            <br />
            <asp:Label ID="lbSeller" runat="server" Text="Label" />
            <br />
            <br />
            Left time:
            <a class="time-time">
                <asp:Label ID="lbLeftDay" runat="server" Text='<%# Eval("LeftDays") %>'></asp:Label>
                <span id="day"></span>
            </a>
            day 
            <a class="time-time">
                <asp:Label ID="lbLeftHours" runat="server" Text='<%# Eval("LeftHours") %>'></asp:Label>
                <span id="hour"></span>
            </a>
            :<a class="time-time">
                <asp:Label ID="lbLeftMinutes" runat="server" Text='<%# Eval("LeftMinutes") %>'></asp:Label>
                <span id="minute"></span>
            </a>
            :<a class="time-time">
                <asp:Label ID="lbLeftSeconds" runat="server" ClientIDMode="Static"></asp:Label>
                <span id="second"></span>
            </a>
            <br />
            <br />
            <asp:TextBox ID="txtBidPrice" runat="server" Width="20%" />
            (Only use for normal auction type)
            <br />
            <br />
            <%--<asp:ImageButton ID="ibtnBid" runat="server" ImageUrl="~/Images/bidHere.jpg" BorderStyle="Double" />--%>
            <input type="button" value="Bid here" id="ibtnBid" style="display: <%if ((Request.Cookies["UserInfo"] != null)&&(!isSeller))
                                                                                     Response.Write("block");
                                                                                 else Response.Write("none"); %>" />
            <asp:Label ID="lbIncrement" runat="server" ClientIDMode="Static"></asp:Label>
        </div>
        <br />
        <div style="float: left; display: inline; width: 100%; height: auto; margin-top: 10px;">
            <asp:Label ID="lbItemDescription" runat="server" Text="Label" />
        </div>
    </div>
</asp:Content>
